package common

// ServerResponsibility 服务器职责枚举
type ServerResponsibility string

const (
	// LOGIN 登录服务
	LOGIN ServerResponsibility = "LOGIN"
	// GAME 游戏逻辑服务
	GAME ServerResponsibility = "GAME"
	// CHAT 聊天服务
	CHAT ServerResponsibility = "CHAT"
	// RANK 排行榜服务
	RANK ServerResponsibility = "RANK"
)

// GameServerInfo 游戏服务器信息
type GameServerInfo struct {
	ServerId         uint32                 `json:"serverId"`
	ServiceAddr      string                 `json:"serverAddr"`
	Responsibilities []ServerResponsibility `json:"responsibilities"`
	MaxConnections   uint32                 `json:"maxConnections"`
	CurrentLoad      uint32                 `json:"currentLoad"`
}

// CanHandle 检查服务器是否能处理指定职责
func (info *GameServerInfo) CanHandle(responsibility ServerResponsibility) bool {
	for _, r := range info.Responsibilities {
		if r == responsibility {
			return true
		}
	}
	return false
}

// GetLoadRatio 获取负载比率
func (info *GameServerInfo) GetLoadRatio() float64 {
	if info.MaxConnections == 0 {
		return 0
	}
	return float64(info.CurrentLoad) / float64(info.MaxConnections)
}
